package summarizer.newversion;

import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import thesis.DataObject;
import thesis.Summary;

import clustering.PinakiCluster;
import clustering.PinakiClusteringAlgorithm;

public class ClusterSummarizer extends Summarizer {

	private final boolean randomSelection = false;

	public String toString() {
		return "Cluster Summarizer";
	}

	private HashMap<Long, DataObject> memoryTweets;

	@Override
	public Summary computeSummary(int summaryDimension, int numberOfPages,
			HashMap<Long, DataObject> tweets, int dimSize) {
		executionTime = (new GregorianCalendar()).getTimeInMillis();
		this.memoryTweets = tweets;
		Set<DataObject> allTweets = new HashSet<DataObject>();
		for (DataObject t : memoryTweets.values()) {
			allTweets.add(t);
		}
		PinakiClusteringAlgorithm cluster = new PinakiClusteringAlgorithm(
				summaryDimension, allTweets);
		Set<PinakiCluster> result = cluster.cluster();
		Summary summary = new Summary();
		if (!randomSelection) {
			for (PinakiCluster c : result) {
				Set<DataObject> group = c.getClusterItems();
				double bestQ = -1;
				DataObject bestT = null;
				for (DataObject t : group) {
					if (t.getQuality() > bestQ) {
						bestT = t;
						bestQ = t.getQuality();
					}
				}
				summary.addMemoryTweet(bestT);
			}
		} else {
			for (PinakiCluster c : result) {
				Set<DataObject> group = c.getClusterItems();
				int ind = new Random().nextInt(group.size());
				int count = 0;
				for (DataObject t : group) {
					if (count == ind) {
						summary.addMemoryTweet(t);
						break;
					}
					count++;
				}

			}
		}
		executionTime = (new GregorianCalendar()).getTimeInMillis()
				- executionTime;
		return summary;
	}

	public ClusterSummarizer() {
		rndAlgo = true;
	}

	@Override
	public long getExecutionTime() {
		// TODO Auto-generated method stub
		return executionTime;
	}

}
